package com.light.wanting.emoji.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonView;
import com.light.applet.common.result.PageView;
import com.light.applet.common.result.Result;
import com.light.wanting.emoji.Content;
import com.light.wanting.emoji.service.ContentService;
import com.light.wanting.emoji.vo.ContentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 魏彪
 */
@RestController
@RequestMapping("/content")
@CrossOrigin
public class ContentController {

    @Autowired
    private ContentService contentService;

    /**
     * <p>Title : findPage</p>
     * <p>Description : Content分页搜索实现</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return Page<ContentVo>
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    @JsonView(Content.ContentHomeView.class)
    public Result<PageView<Content>> findPage(@RequestBody(required = false) Content content, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用ContentService实现分页条件查询Content
        IPage<Content> pageInfo = contentService.findPage(new Page<>(pageNum, pageSize), content);
        PageView<Content> pageView = new PageView<>();
        BeanUtils.copyProperties(pageInfo, pageView);
        return Result.ok(pageView);
    }

    /**
     * <p>Title : findById</p>
     * <p>Description : 根据ID查询Content数据</p>
     *
     * @param id 主键
     * @return ContentVo
     */
    @GetMapping(value = "/{id}")
    @JsonView({Content.ContentDetailsView.class})
    public Result<Content> findById(@PathVariable Long id) {
        // 调用ContentService实现根据主键查询Content
        Content content = contentService.findById(id);
        return Result.ok(content);
    }

    /**
     * 用户点赞
     *
     * @param id 内容编号
     * @return 点赞数量
     */
    @GetMapping(value = "/like/{id}")
    public Result<ContentVo> like(@PathVariable Long id) {
        ContentVo contentVo = contentService.like(id);
        return Result.ok(contentVo);
    }

    /**
     * 用户收藏
     *
     * @param id 内容编号
     * @return 收藏数
     */
    @GetMapping(value = "/collect/{id}")
    public Result<ContentVo> collect(@PathVariable Long id) {
        ContentVo contentVo = contentService.collect(id);
        return Result.ok(contentVo);
    }

    /**
     * <p>Title : liked</p>
     * <p>Description : 我的点赞</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return Page<ContentVo>
     */
    @GetMapping(value = "/liked/{pageNum}/{pageSize}")
    public Result<IPage<ContentVo>> liked(@PathVariable int pageNum, @PathVariable int pageSize) {
        IPage<ContentVo> contentVo = contentService.liked(new Page<>(pageNum, pageSize));
        return Result.ok(contentVo);
    }

    /**
     * <p>Title : collected</p>
     * <p>Description : 我的收藏</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return Page<ContentVo>
     */
    @GetMapping(value = "/collected/{pageNum}/{pageSize}")
    public Result<IPage<ContentVo>> collected(@PathVariable int pageNum, @PathVariable int pageSize) {
        IPage<ContentVo> contentVo = contentService.collected(new Page<>(pageNum, pageSize));
        return Result.ok(contentVo);
    }

}
